package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * model.User: DELL
 * Date: 2023-03-22
 * Time: 17:24
 */

// 通过该表封装 用户中的基本操作
public class UserDao {

    // 1. 根据 userId 查询用户信息
    public User selectById(int userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 sql 语句
            String sql = "select * from user where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            // 3. 执行sql
            resultSet = statement.executeQuery();
            // 4. 遍历结果集合
            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 2. 根据 username 来查询用户
    public User selectByUsername(String username) {

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 sql 语句
            String sql = "select * from user where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            // 3. 执行sql
            resultSet = statement.executeQuery();
            // 4. 遍历结果集合
            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setName(resultSet.getString("name"));
                user.setCount(resultSet.getInt("count"));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
    // 你自己试试 .刷新就好 他不乱 你看不来我的 api 名

    // 新增一个用户
    public void add(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        // 1. 建立连接
        // 2. 构造 sql 语句
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into user values(null, ?, ?, ?, ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUsername());
            statement.setString(2, user.getPassword());
            statement.setString(3, user.getName());
            statement.setInt(4, user.getCount());

            // 3. 执行 sql
            int ret = statement.executeUpdate();
            // System.out.println(ret);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 4. 关闭资源
            DBUtil.close(connection, statement, null);
        }
    }

    // 根据用户名查询数据库
    public int isExist(String username, User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1. 建立链接
            connection = DBUtil.getConnection();
            // 2. 构造 sql 语句
            String sql = "select * from user where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUsername());

            // 3. 执行 sql
            resultSet = statement.executeQuery();

            // 4. 遍历结果结合
            if(resultSet.next()) {
                return 1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
     return 0;
    }



    // 根据密码查询用户
//    public User searchByPassword(String password) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        ResultSet resultSet = null;
//
//        try {
//            // 1. 建立连接
//            connection = DBUtil.getConnection();
//            // 2. 构造 sql 语句
//            String sql = "select * from user where password = ?";
//            statement = connection.prepareStatement(sql);
//            statement.setString(1, password);
//            // 3. 执行 sql
//            resultSet = statement.executeQuery();
//            // 4. 遍历结果集合
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
